#!/usr/bin/python
import sqlite3
import os
import cgitb
cgitb.enable()

# Create gallery database
conn = sqlite3.connect(os.environ["DOCUMENT_ROOT"] 
    + "/PictureShare/pygallery.sqlite")
cur = conn.cursor()
cur.executescript("""

drop table if exists albums;
drop table if exists pictures;
drop table if exists pictures_deleted;
drop table if exists users;
drop table if exists user_sessions;
drop table if exists user_activation;

create table albums
(
  album_id INTEGER primary key, 
  user_id int, 
  name varchar(256),
  is_public int
);

create table pictures
(
  picture_id INTEGER primary key, 
  album_id int, 
  user_id int, 
  file_name varchar(512),
  label varchar(512)
);

create table pictures_deleted
(
  picture_id INTEGER primary key, 
  album_id int, 
  user_id int, 
  file_name varchar(512),
  label varchar(512) 
);

create table users
(
  user_id INTEGER primary key, 
  username varchar(512), 
  password varchar(56), 
  activated int
);

create table user_activation
(
  user_id INTEGER primary key,
  activation_id varchar(56)
);

create table user_sessions
(
  user_session_id varchar(56) primary key,
  user_id int,
  login_time int
);

insert into users values (
  1,
  "george@cs.purdue.edu",
  "2aa7cd1da1c53c16703212441fcad8214d8ea5df3f6a78a390b3a5c7",
  1
);
insert into users values (
  2,
  "mary@cs.purdue.edu",
  "7a6cef732a45531f3bcb6367da36d25090fd9b4add1554d4f76973f6",
  1
);
insert into users values (
  3,
  "peter@cs.purdue.edu",
  "8e4345dd1379abd5cb0836cb42305178dc15dd71b6159c4e2ebeec9b",
  1
);

insert into albums values (NULL, 1, "I Love Corgis", 0);
insert into albums values (NULL, 2, "Space!", 1);
insert into albums values (NULL, 3, "Some nature pics", 1);

-- User 1
insert into pictures values (NULL, 1, 1, 
  "corgi.jpg", 
  "corgi.jpg");
insert into pictures values (NULL, 1, 1, 
  "dogs-corgi4.jpg", 
  "dogs-corgi4.jpg");
insert into pictures values (NULL, 1, 1, 
  "teddy_welsh_corgi_01_w450.jpg", 
  "dogs-corgi4.jpg");
-- User 2
insert into pictures values (NULL, 2, 2,
  "ISIS-space.jpg",
  "ISIS-space.jpg");
insert into pictures values (NULL, 2, 2,
  "space-shuttle-challenger.jpg",
  "space-shuttle-challenger.jpg");
insert into pictures values (NULL, 2, 2,
  "sunset-space-pacific-ocean-thumb.jpg",
  "sunset-space-pacific-ocean-thumb.jpg");
-- User 3
insert into pictures values (NULL, 3, 3,
  "nature6.jpg",
  "nature6.jpg");
insert into pictures values (NULL, 3, 3,
  "rupi-8744-nature-beauty.jpg",
  "rupi-8744-nature-beauty.jpg");
insert into pictures values (NULL, 3, 3,
  "serene-nature-walk-trail.jpg",
  "serene-nature-walk-trail.jpg");

""")
conn.commit()
cur.close()
conn.close()
print("Content-type: text/html")
print("")
print("Created db")
